package com.wzq.animal.back.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wzq.animal.back.controller.dto.UserDTO;
import com.wzq.animal.back.mapper.PermissionMapper;
import com.wzq.animal.back.mapper.RolePermissionMapper;
import com.wzq.animal.back.mapper.UserMapper;
import com.wzq.animal.back.pojo.Permission;
import com.wzq.animal.back.pojo.RolePermission;
import com.wzq.animal.back.pojo.User;
import com.wzq.animal.back.service.UserService;
import com.wzq.animal.utils.CipherUtils;
import com.wzq.animal.utils.TokenUtils;
import com.wzq.animal.common.exception.GlobalException;
import com.wzq.animal.common.exception.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public UserDTO checkLogin(UserDTO userDTO) {
        String username = userDTO.getUsername();
        String password = CipherUtils.encrypt(userDTO.getPassword());
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username).eq(User::getPassword,password);
        User user = null;
        try {
            user = userMapper.selectOne(queryWrapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new GlobalException(StatusCode.ERROR);
        }

        if(user != null){
            List<Permission> userPermissions = getUserPermissions(user.getRoleId());
            String token = TokenUtils.genToken(user);
            BeanUtil.copyProperties(user,userDTO,true);
            userDTO.setToken(token);
            userDTO.setPermissions(userPermissions);
            return userDTO;
        }else{
            throw new GlobalException(StatusCode.LOGIN_USERNAME_PASSWORD);
        }
    }

    @Override
    public User checkUsername(String username) {
        UserDTO userDTO = new UserDTO();
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,username);
        User user = null;
        try {
            user = userMapper.selectOne(queryWrapper);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new GlobalException(StatusCode.ERROR);
        }
        if(user != null){
            //BeanUtil.copyProperties(user,userDTO,true);
            return user;
        }else{
            throw new GlobalException(StatusCode.LOGIN_USERNAME);
        }
    }

    @Override
    public User checkRegister(User user) {
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        List<User> users = userMapper.selectList(queryWrapper);
        if(users.size() > 0){
            throw new GlobalException(StatusCode.REGISTER_USERNAME);
        }else{
            userMapper.insert(user);
            user = userMapper.selectOne(queryWrapper);
        }
        return user;
    }

    public List<Permission> getUserPermissions(Integer roleId){
        LambdaQueryWrapper<RolePermission> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RolePermission::getRoleId,roleId);
        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(queryWrapper);
        List<Integer> listPermissionIds = new ArrayList<>();
        for(RolePermission rp : rolePermissions){
            listPermissionIds.add(rp.getPermissionId());
        }
        List<Permission> parentNode = new ArrayList<>();
        //获取父菜单
        for(Integer i : listPermissionIds){
            Permission one = permissionMapper.selectById(i);
            if(one.getPid() == null){
                parentNode.add(one);
            }
        }
        //子菜单填入父菜单
        for(int i = 0;i < parentNode.size();i++){
            List<Permission> children = new ArrayList<>();
            for(Integer j : listPermissionIds){
                Permission one = permissionMapper.selectById(j);
                if(one.getPid() != null && one.getPid() == parentNode.get(i).getId()){
                    children.add(one);
                }
            }
            parentNode.get(i).setChildren(children);
        }
        return parentNode;
    }
}
